const menuShowDialogTs = ref<boolean>(false);
const splitShowDialog = ref<boolean>(false);
const dataList = ref<Namespace.commandRegion>();
const isState = ref<string>('');
const dataTree = ref<string>(); // id
const notice = ref<boolean>(false);
import { deletePost } from '@/api/commandPages';
import qs from 'qs';
const deleteFunction = async (data) => {
  if (data.hasChildren) {
    ElMessage.error('存在子级，请先删除子级');
    return false;
  }
  const JsonData = {
    id: data.id,
  };
  const { code } = await deletePost(qs.stringify(JsonData));
  if (code === 0) {
    ElMessage.success('删除成功');
    notice.value = true;
  } else {
    ElMessage.error('删除失败');
    notice.value = false;
  }
  dataTree.value = data.parentId;
};
function editTree(data) {
  isState.value = 'edit';
  menuShowDialogTs.value = true;
  dataList.value = data;
  dataTree.value = data.id;
}
function addTree(data) {
  isState.value = 'add';
  menuShowDialogTs.value = true;
  dataList.value = data;
  dataTree.value = data.id;
}

/**
 * 两列表单数据
 * 权限单元
 */
const formData = reactive<Namespace.FormType>({
  loadKey: true,
  formFields: {
    parentId: '',
    permissionName: '',
  },
  formRules: [
    [
      {
        title: '父级单元',
        required: true,
        field: 'parentId',
        dataKey: 'value',
        dataName: 'label',
        default: '',
        placeholder: '请选择父级单元',
        type: 'select',
        colSize: 12,
        data: [],
      },
    ],
    [
      {
        title: '名称',
        required: true,
        field: 'permissionName',
        placeholder: '请输入名称',
        type: 'text',
        colSize: 12,
      },
    ],
  ],
});
const formSplitData = reactive<Namespace.FormType>({
  loadKey: true,
  formFields: {
    parentId: '',
    parentName: '',
    splitName: '',
    origId: '',
    perIds: '',
  },
  formRules: [
    [
      {
        title: '父级名称',
        required: true,
        field: 'parentName',
        placeholder: '请输入父级名称',
        type: 'text',
        colSize: 12,
      },
    ],
    [
      {
        title: '拆分名称',
        required: true,
        field: 'splitName',
        placeholder: '请输入拆分名称',
        type: 'text',
        colSize: 12,
      },
    ],
  ],
});

const rightMenuList = reactive<Array<Namespace.unitRightMenu>>([
  {
    iconName: 'add',
    name: '新增',
    size: 18,
    isShown: true,
    function: addTree,
  },
  {
    iconName: 'edit',
    name: '编辑',
    size: 18,
    isShown: true,
    function: editTree,
  },
  {
    iconName: 'jstree-delete',
    name: '删除',
    size: 18,
    isShown: true,
    function: deleteFunction,
  },
]);

export {
  rightMenuList,
  menuShowDialogTs,
  dataList,
  formData,
  notice,
  isState,
  dataTree,
  formSplitData,
  splitShowDialog,
};
